package com.example.forum.Controller;
/* @Author      zouzh
 * @PackageName forum
 * @Package     com.example.forum.Controller
 * @Date        2023/9/26 19:28
 */

import com.example.forum.common.AppConfig;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.service.IArticleReplyService;
import com.example.forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/creat")
    public AppResult creat(HttpServletRequest request,
                           @ApiParam("帖子 id") @RequestParam("articleId") @NonNull Long articleId,
                           @ApiParam("帖子内容") @RequestParam("content") @NonNull String content){

        // 校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            // 表示用户已禁言
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        // 查询帖子
        Article article = articleService.selectById(articleId);
        // 帖子状态
        if (article == null || article.getDeleteState() == 1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 是否封帖
        if (article.getState() == 1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }

        // 构造回复帖子对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);

        articleReplyService.create(articleReply);

        return AppResult.success();
    }

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticle(@ApiParam("帖子 id") @RequestParam("articleId") @NonNull Long articleId){
        List<ArticleReply> articleReplies;

        // 检验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1 ){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if (article.getState() == 1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }

        // 调用service
         articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null){
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
}
